package com.zq.server.api.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zq.common.core.domain.Result;
import com.zq.common.exception.base.BaseException;
import com.zq.common.utils.SecurityUtils;
import com.zq.common.utils.StringUtils;
import com.zq.server.api.dao.ApiTeamMapper;
import com.zq.server.api.dao.ApiTeamMemberMapper;
import com.zq.server.api.domain.entity.ApiTeam;
import com.zq.server.api.domain.entity.ApiTeamMember;
import com.zq.server.api.service.IApiTeamService;

/**
 * 战队功能模块Service业务层处理
 * 
 * @author 财哥
 * @date 2024-03-31
 */
@Service
public class ApiTeamServiceImpl implements IApiTeamService {
    @Autowired
    private ApiTeamMapper apiTeamMapper;

    @Autowired
    private ApiTeamMemberMapper apiTeamMemberMapper;

    /**
     * 查询战队功能模块
     * @param id 战队功能模块主键
     * @return 战队功能模块
     */
    @Override
    public ApiTeam selectApiTeamById(Long id) {
        return apiTeamMapper.selectApiTeamById(id);
    }

    /**
     * 查询战队功能模块列表
     * @param apiTeam 战队功能模块
     * @return 战队功能模块
     */
    @Override
    public List<ApiTeam> selectApiTeamList(ApiTeam apiTeam) {
        return apiTeamMapper.selectApiTeamList(apiTeam);
    }
    /**
     * 新增战队功能模块
     * @param apiTeam 战队对象
     * @return 操作结果
     */
    @Override
    @Transactional(rollbackFor = BaseException.class)
    public Result addTeam(ApiTeam apiTeam) {
        validateParameters(apiTeam);
        ApiTeam response = createApiTeam(apiTeam);
        createApiTeamMember(response);
        return Result.success();
    }






    /**
     * 校验参数是否合法
     * @param team 待校验的战队对象
     */
    private void validateParameters(ApiTeam team) {
        if (StringUtils.isEmpty(team.getBio()) || StringUtils.isEmpty(team.getCity())
                || StringUtils.isEmpty(team.getImg()) || StringUtils.isEmpty(team.getName())) {
            throw new BaseException("10020", "参数不合法");
        }
    }


    /**
     * 构建战队对象并插入数据库
     * @param apiTeam 待构建的战队对象
     * @return 已插入数据库的战队对象
     */
    private ApiTeam createApiTeam(ApiTeam apiTeam) {
        LocalDateTime currentTime = LocalDateTime.now();
        apiTeam.setCreationTime(currentTime);
        apiTeam.setUpdateTime(currentTime);
        apiTeam.setFramerId(SecurityUtils.getWechatUserId());
        apiTeam.setFramerName(SecurityUtils.getWechatName());
        apiTeamMapper.insertApiTeam(apiTeam);
        return apiTeam;
    }


    /**
     * 构建战队成员并插入数据库
     * @param apiTeam 已创建的战队对象
     */
    private void createApiTeamMember(ApiTeam apiTeam) {
        ApiTeamMember apiTeamMember = new ApiTeamMember();
        apiTeamMember.setTeamId(apiTeam.getId());
        apiTeamMember.setRole(1);
        apiTeamMember.setUserId(apiTeam.getFramerId());
        LocalDateTime currentTime = LocalDateTime.now();
        apiTeamMember.setUpdateTime(currentTime);
        apiTeamMember.setCreateTime(currentTime);
        apiTeamMemberMapper.insertApiTeamMember(apiTeamMember);
    }

    /**
     * 修改战队功能模块
     * 
     * @param apiTeam 战队功能模块
     * @return 结果
     */
    @Override
    public int updateApiTeam(ApiTeam apiTeam) {
        apiTeam.setUpdateTime(LocalDateTime.now());
        return apiTeamMapper.updateApiTeam(apiTeam);
    }

    /**
     * 批量删除战队功能模块
     * 
     * @param ids 需要删除的战队功能模块主键
     * @return 结果
     */
    @Override
    public int deleteApiTeamByIds(Long[] ids) {
        return apiTeamMapper.deleteApiTeamByIds(ids);
    }

    /**
     * 删除战队功能模块信息
     * 
     * @param id 战队功能模块主键
     * @return 结果
     */
    @Override
    public int deleteApiTeamById(Long id) {
        return apiTeamMapper.deleteApiTeamById(id);
    }
}
